﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;

namespace TakaGUI.IO
{
	public class XmlTree : Dictionary<string, XmlTree>
	{
		public Dictionary<string, string> Attributes = new Dictionary<string, string>();
		public string Name;
		public string Value;

		public XmlTree()
		{
		}
		public XmlTree(string xmlText)
		{
			TextReader textReader = new StringReader(xmlText);
			using (XmlReader xmlReader = XmlReader.Create(textReader))
			{
				Load(xmlReader);
			}
			textReader.Close();
		}
		public XmlTree(FileStream fs)
		{
			TextReader textReader = new StreamReader(fs);
			using (XmlReader xmlReader = XmlReader.Create(textReader))
			{
				Load(xmlReader);
			}
			textReader.Close();
		}
		public XmlTree(XmlReader reader)
		{
			Load(reader);
		}

		public void Load(XmlReader reader)
		{
			reader.MoveToContent();
			Name = reader.Name;

			while (reader.MoveToNextAttribute())
			{
				if (reader.NodeType == XmlNodeType.Attribute)
					Attributes.Add(reader.Name, reader.Value);
			}

			while (reader.Read())
			{
				switch (reader.NodeType)
				{
					case XmlNodeType.Element:
						Add(reader.Name, new XmlTree(reader.ReadOuterXml()));
						break;
					case XmlNodeType.Text:
						Value = reader.Value;
						break;
				}
			}

			reader.Close();
		}

		public int GetInt(string key)
		{
			return Convert.ToInt32(this[key]);
		}
		public double GetDouble(string key)
		{
			return Convert.ToDouble(this[key]);
		}

	}
}
